package com.ZYHsheep.dao;

import com.ZYHsheep.entity.Castrate;
import com.ZYHsheep.util.JDBCUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CastrateDao {
    /*新增去势*/
    public int insert(Castrate c, Connection conn) throws SQLException {
        String sql = "INSERT INTO castrate(Sno, technician, event_time) VALUES (?,?,?)";
        try (PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, c.getSno());
            ps.setString(2, c.getTechnician());
            ps.setTimestamp(3, Timestamp.valueOf(c.getEventTime()));
            return ps.executeUpdate();
        }
    }

    /*去势记录 */
    public List<Castrate> findAll() {
        List<Castrate> list = new ArrayList<>();
        String sql = "SELECT * FROM castrate ORDER BY event_time DESC";
        try (Connection conn = JDBCUtil.getConn();
             PreparedStatement ps = conn.prepareStatement(sql);
             ResultSet rs = ps.executeQuery()) {
            while (rs.next()) {
                Castrate r = new Castrate();
                r.setId(rs.getInt("id"));
                r.setSno(rs.getInt("Sno"));
                r.setTechnician(rs.getString("technician"));
                r.setEventTime(rs.getTimestamp("event_time").toLocalDateTime());
                list.add(r);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
